import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';
import { useMessage } from "/@/hooks/web/useMessage";
const { createMessage: $message } = useMessage();

enum Api {
  list = '/full/seal/list',
  save='/full/seal/add',
  edit='/full/seal/edit',
  deleteOne = '/full/seal/delete',
  deleteBatch = '/full/seal/deleteBatch',
  importExcel = '/full/seal/importExcel',
  exportXls = '/full/seal/exportXls',
  updateStat = "/full/seal/updateStat",
  queryById = "/full/seal/queryById",
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({url: Api.list, params});

/**
 * 通过ID查询获得
 * @param params
 */
export const queryById = (params) => defHttp.get({url: Api.queryById, params}, {isTransformResponse:false});

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.get({url: Api.deleteOne, params}, {joinParamsToUrl: true, isTransformResponse: false}).then((res) => {
    if (res.success) {
      handleSuccess()
      $message.success("操作成功！")
    } else {
      $message.error(res.message)
    }
  });
}

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true, isTransformResponse: false}).then((res) => {
        if (res.success) {
          $message.success("操作成功！")
          handleSuccess()
        } else {
          $message.error(res.message)
        }
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 * @param isUpdate 是否是更新数据
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

/**
 * 状态
 * @param params
 */
export const updateStat = (params, handleSuccess) => {
  return defHttp.post({ url: Api.updateStat, data: params }, { joinParamsToUrl: true, isTransformResponse: false }).then((res) => {
    if (res.success) {
      handleSuccess()
      $message.success(res.message)
    } else {
      $message.error(res.message)
    }
  });
}